<template>
	<view class="container">
		<view class="searchContainer">
			<uni-search-bar placeholder="请输入您想查看的课程" bgColor="#FFFFFF" placeholderColor="#D8D8D8" radius=18
				@confirm="searchHadle()" cancel-text="取消">
				<uni-icons slot="searchIcon" color="#D8D8D8" size="18" type="search" />
			</uni-search-bar>
		</view>
		<view class="screenContainer">
			<view class="screenHead">
				<view :class="['screenItem',{'ac':index===screenIndex}]" @click="screenHadle(index)" v-for="(item,index) in screenList" :key="index">
					{{item.name}}
					<view  :class="['icon',{'active':screenIndex===index}]" ></view>
				</view>
			</view>
		</view>
		<view v-if="list.length===0">
			<luanqing-empty :show="true" text="没有更多数据啦" textColor="#000"></luanqing-empty>
		</view>
		<view class="item" v-for="(item,index) in list" :key="index">
			<navigator :url="'./detail?goods_id='+item.goods_id" hover-class="none">
				<view class="detail">
					<view class="pic">
						<image :src="item.thumb" mode=""></image>
					</view>
					<view class="intro">
						<view class="title">
							<text>{{item.goods_name}}</text>
						</view>
						<view class="jigou">
							<text>{{item.shop_name}}</text>
						</view>
						<view class="time">
							<image src="../../static/img/shijian.png" mode=""></image>
							<text>上课时间: {{item.class_time}}</text>
						</view>
						<view class="desc">
							<image src="../../static/img/xiangqing.png" mode=""></image>
							<text>{{item.content}}</text>
						</view>
					</view>
				</view>
				<view class="price">
					<text class="icon">¥</text>
					<text class="num">{{item.price}}</text>
				</view>
			</navigator>
		</view>
		<uni-popup ref="popup" type="top">
			<view class="headContainer">
				<view class="searchContainer">
					<uni-search-bar placeholder="请输入您想查看的课程" bgColor="#FFFFFF" placeholderColor="#D8D8D8" radius=18
						@confirm="searchHadle()" cancel-text="取消">
						<uni-icons slot="searchIcon" color="#D8D8D8" size="18" type="search" />
					</uni-search-bar>
				</view>
				<view class="screenContainer">
					<view class="screenHead">
						<view :class="['screenItem',{'ac':index===screenIndex}]" 
						v-for="(item,index) in screenList" 
						@click="screenHadle(index)"
							:key="index">
							{{item.name}}
							<view  :class="['icon',{'active':screenIndex===index}]" ></view>
						</view>
					</view>
					<view class="screenContent">
						<view :class="['childItem',{'childItemAc':screenItemIndex===index}]" 
						v-for="(item,index) in screenList[screenIndex].child" 
						@click="screenItemHadle(item,index)"
						:key="index">
							{{item.name}}
						</view>
					</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [],
				page: 1,
				pagesize: 20,
				loadStatus: 'loading', //加载样式：more-加载前样式，loading-加载中样式，nomore-没有数据样式
				isLoadMore: false, //是否加载中
				token: "",
				cate_id: "",
				shop_id: "",
				search: "",
				screenList: [],
				screenIndex: null,
				screenItemIndex:null,
				distance:"",
				price:"",
			}
		},
		onLoad: function(option) { //option为object类型，会序列化上个页面传递的参数
			console.log(option); //打印出上个页面传递的参数。
			this.cate_id = option.cate_id?option.cate_id:"";
			this.shop_id = option.shop_id?option.shop_id:'';
			uni.getStorage({
				key: 'user_info',
				success: (res) => {
					this.token = res.data.token;
					this.getlist();
					this.getGoodsListTag();
				},
				fail:()=> {
					this.init();
				}
			});
		},
		onReachBottom() { //上拉触底函数
			if (!this.isLoadMore) { //此处判断，上锁，防止重复请求
				this.isLoadMore = true
				this.page += 1
				this.getlist()
			}
		},
		methods: {
			init() {
				uni.getStorage({
					key: 'user_info',
					success: (res) => {
						this.token = res.data.token;
						if (this.token) {
							uni.getLocation({
								type: 'wgs84',
								success: (res) => {
									let longitude = res.longitude;
									let latitude = res.latitude;
									uni.request({
										url: 'https://mx.acktechnologies.com.cn/api/wxapp/apply/uploadLocation',
										data: {
											"token": this.token,
											"longitude": longitude,
											"latitude": latitude
										},
										method: 'GET',
										success: res => {
											console.log(res);
											
										},
										fail: (res) => {
											console.log(res)
											this.wxLogin();
										},
									});
								},
								fail: () => {
									this.wxLogin();
								}
							});
						} else {
							this.wxLogin();
						}
					},
					fail: () => {
						this.wxLogin();
					}
				});
			},
			wxLogin() {
				uni.login({
					provider: 'weixin',
					success: (loginRes) => {
						console.log(loginRes)
						this.code = loginRes.code;
						this.$request('/api/wxapp/index/wxLogin', {
							'code': loginRes.code
						}).then(wxLoginRes => {
							if (wxLoginRes.code === 1) {
								this.isReg = wxLoginRes.data.is_reg;
								this.session_key = wxLoginRes.data.session_key;
								this.openid = wxLoginRes.data.openid;
								uni.setStorageSync('openid', this.openid);
								if (this.isReg) {
									this.auth();
								} else {
									uni.redirectTo({
										url: '../login/auth?redirect=../course/list'
									});
								}
							}
						})
					}
				});
			},
			auth() {
				let param = {
					'openid': this.openid
				}
				this.$request('/api/wxapp/index/accountLogin', param)
					.then(res => {
						let userinfo = res.data.userinfo;
						console.log(userinfo)
						uni.setStorage({
							key: 'user_info',
							data: userinfo,
							success: () => {
								console.log('success');
								this.token = userinfo.token;
								this.getlist();
								this.getGoodsListTag();
							}
						});
						if (res.data.usernum > 1) {
							uni.navigateTo({
								url: '../login/user_select'
							});
						}
					})
			},
			screenHadle(index) {
				this.$refs.popup.open();
				this.screenIndex = index;
				this.screenItemIndex = null;
			},
			searchHadle(e) {
				this.search = e.value;
				this.list = [];
				this.getlist();
			},
			screenItemHadle(obj,index){
				this.screenItemIndex = index;
				this.$refs.popup.close();
				if(obj.type==='category'){
					this.cate_id = obj.value;
				}else if(obj.type==='distance'){
					this.distance = obj.value;
				}else{
					this.price = obj.value;
				}
				this.list = [];
				this.getlist();
			},
			getGoodsListTag() {
				uni.request({
					url: `https://mx.acktechnologies.com.cn/api/wxapp/apply/getGoodsListTag`,
					data: {
						"token": this.token,
						"cate_id": this.cate_id
					},
					method: 'GET',
					success: res => {
						if (res.data.code === 1) {
							this.screenList = res.data.data;
						}
					}
				})
			},
			getlist() {
				let param = {
					"token": this.token,
					"page": this.page,
					"pageSize": 20,
					"shop_id": this.shop_id,
				}
				if(this.cate_id){
					param.cate_id = this.cate_id
				}
				if(this.search){
					param.search = this.search
				}
				if(this.distance){
					param.distance = this.distance
				}
				if(this.price){
					param.price = this.price
				}
				
				uni.request({
					url: `https://mx.acktechnologies.com.cn/api/wxapp/apply/getGoodsInfoList`,
					data: param,
					method: 'GET',
					success: res => {
						console.log(res)
						if (res.data.code == 1) {
							if (res.data.data) {
								this.list = this.list.concat(res.data.data)
								if (res.data.data.length < this.pagesize) { //判断接口返回数据量小于请求数据量，则表示此为最后一页
									this.isLoadMore = true
									this.loadStatus = 'nomore'
								} else {
									this.isLoadMore = false
								}
							} else {
								this.isLoadMore = true
								this.loadStatus = 'nomore'
							}
						} else { //接口请求失败的处理
							uni.showToast({
								title: res.data.msg,
								icon: 'none'
							})
							this.isLoadMore = false
							if (this.page > 1) {
								this.page -= 1
							}
						}
					},
					fail: () => { //接口请求失败的处理
						uni.showToast({
							title: '服务器开小差了呢，请您稍后再试',
							icon: 'none'
						})
						this.isLoadMore = false
						if (this.page > 1) {
							this.page -= 1
						}
					},
				});
			},
		}
	}
</script>

<style scoped lang="scss">
	.container {
		width: 100%;
		height: 100%;
		overflow: auto;
		padding-bottom: 20px;
	}

	.headContainer {
		background-color: #FFFFFF;
		overflow: hidden;
	}

	.item {
		width: 670rpx;
		background: #FFFFFF;
		box-shadow: 0px 4rpx 20rpx 0px #E9EBF6;
		border-radius: 16rpx;
		margin: 20rpx auto 0 auto;
		padding: 20rpx 18rpx;
		box-sizing: border-box;

		.detail {
			display: flex;
			flex-direction: row;
			justify-content: flex-start;

			.pic {
				width: 200rpx;
				height: 200rpx;
				flex-shrink: 0;
				margin-right: 22rpx;

				image {
					display: block;
					width: 100%;
					height: 100%;
					border-radius: 20rpx;
				}
			}

			.title {
				font-size: 16px;
				font-family: SourceHanSansCN-Medium, SourceHanSansCN;
				font-weight: 700;
				color: #333333;
			}

			.jigou {
				display: inline-block;
				padding: 3rpx 5rpx;
				font-size: 13px;
				font-family: SourceHanSansCN-Normal, SourceHanSansCN;
				font-weight: 400;
				color: #66A8A0;
				background: #FFFFFF;
				border-radius: 2px;
				border: 1px solid #C1E0DC;
				margin-top: 20rpx;
			}

			.time {
				display: flex;
				flex-direction: row;
				align-items: center;
				margin-top: 10rpx;

				image {
					width: 26rpx;
					height: 24rpx;
				}

				text {
					font-size: 13px;
					font-family: SourceHanSansCN-Normal, SourceHanSansCN;
					font-weight: 400;
					color: #666666;
					margin-left: 10rpx;
				}
			}

			.desc {
				display: flex;
				flex-direction: row;
				align-items: center;
				margin-top: 10rpx;

				image {
					width: 26rpx;
					height: 24rpx;
				}

				text {
					width: 320rpx;
					font-size: 13px;
					font-family: SourceHanSansCN-Normal, SourceHanSansCN;
					font-weight: 400;
					color: #666666;
					margin-left: 10rpx;
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap;
				}
			}
		}

		.price {
			height: 68rpx;
			line-height: 68rpx;
			color: #ED6A2A;
			border-top: 1px solid #EAEAEA;
			text-align: right;
			margin-top: 20rpx;

			.icon {
				font-size: 13px;
			}

			.num {
				font-size: 18px;
				margin-left: 5rpx;
			}
		}
	}

	.searchContainer {
		padding: 0 12px;
		box-sizing: border-box;
	}

	.screenContainer {
		padding: 0 40rpx;
		margin-bottom: 20px;
		box-sizing: border-box;

		.screenHead {
			width: 670rpx;
			margin: 0 auto;
			display: flex;
			flex-direction: row;
			justify-content: flex-start;
			align-items: center;
			margin: 5px 0;

			.screenItem {
				margin-right: 40rpx;
				display: flex;
				flex-direction: row;
				justify-content: flex-start;
				align-items: baseline;
				font-size: 15px;
				color: #666666;
				line-height: 15px;

				.icon {
					width: 0;
					height: 0;
					border-bottom: 6px solid #666666;
					border-left: 6px solid transparent;
					margin-left: 3px;
				}

				.active {
					width: 0;
					height: 0;
					border-bottom: 6px solid #48D28C;
					border-left: 6px solid transparent;
					margin-left: 3px;
				}
			}
			.ac{
				color: #48D28C;
			}
		}

		.childItem {
			display: inline-block;
			font-size: 15px;
			line-height: 15px;
			color: #666666;
			padding: 4px 12px;
			border-radius: 13px;
			background: #F6F6F6;
			margin-right: 20px;
			margin-top: 20px;
			box-sizing: border-box;
			border: 1px solid #F6F6F6;
		}
		.childItemAc{
			background-color: #FFFFFF;
			border: 1px solid #48D28C;
		}
	}
</style>
